package com.cjy;
import java.io.IOException;
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.io.LongWritable;
 import org.apache.hadoop.io.NullWritable;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.mapreduce.Job;
 import org.apache.hadoop.mapreduce.Mapper;
 import org.apache.hadoop.mapreduce.Reducer;
 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class FlowSortMapReduce {
    public static class FlowSortMapper extends Mapper<LongWritable, Text, FlowBean, NullWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String line = value.toString();
            String[] fields = StringUtils.split(line, " ");

            String phoneNumber = fields[0];
            long up_flow = Long.parseLong(fields[6]);
            long down_flow = Long.parseLong(fields[7]);

            context.write(new FlowBean(phoneNumber, up_flow, down_flow), NullWritable.get());
        }
    }
    public static class FlowSortReducer extends Reducer<FlowBean, NullWritable, Text, FlowBean>{
        @Override
        protected void reduce(FlowBean key, Iterable<NullWritable> values,Context context)throws IOException, InterruptedException {

            String phoneNumber = key.getPhoneNumber();
            context.write(new Text(phoneNumber), key);
        }

    }
    public static void main(String[] args) throws ClassNotFoundException, IOException, InterruptedException {
        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);
        //设置整个job所用的那些类在哪个jar包
        job.setJarByClass(FlowSortMapReduce.class);

        job.setMapperClass(FlowSortMapper.class);
        job.setReducerClass(FlowSortReducer.class);

        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setOutputKeyClass(Text.class);;
        job.setOutputValueClass(FlowBean.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }
    }